<?php

/**
 * Created by PhpStorm.
 * User: cit
 * Date: 2016/3/10
 * Time: 14:11
 */
include_once dirname(__FILE__) . '\in_mysql.php';
include_once dirname(__FILE__) . '\in_session.php';
include_once dirname(__FILE__) . '\in_teacher.php';
include_once dirname(__FILE__) . '\in_class.php';

include_once dirname(__FILE__) . '\..\common\config.php';

$res = array();

if (isset($_POST['type'])) {
    $type = $_POST['type'];
    switch ($type) {
        case 'getClassInfo':
            $res = GetClassInfo();
            break;
        case 'changeClassName':
            $res = ChangeClassName();
            break;
        case 'changeClassCS':
            $res = ChangeClassCS();
            break;
        case 'dissClass':
            $res = DisClass();
            break;
        case 'addStudent':
            $res = AddStudent();
            break;
        case 'disStudent':
            $res = DisStudent();
            break;
        case 'giveLeader'://转交班主任权利
            $res = GiveLeader();
            break;
        case 'agreeTeacherJoin'://同意老师加入
            $res = AgreeTeacherJoin();
            break;
        case 'disTeacher'://移除老师
            $res = DisTeacher();
            break;
        default:
            $res['code'] = 2;
            $res['msg'] = strval($type_error);
    }
} else {
    $res['code'] = 1;
    $res['msg'] = strval($type_none);
}

echo json_encode($res);


///////////////////////////////////////////////

/**
 * 移除老师
 */
function DisTeacher() {
    $cid = $_POST['cid'];
    $gtid = $_POST['tid'];
    $htid = TEACHER::GetSessionTid();

    $res = myCLASS::DisTeacher($cid, $htid, $gtid);
    return $res;
}

/** 同意老师加入
 * @return mixed
 */
function AgreeTeacherJoin() {
    $cid = $_POST['cid'];
    $gtid = $_POST['tid'];
    $htid = TEACHER::GetSessionTid();

    $res = myCLASS::AgreeTeacherJoin($cid, $htid, $gtid);
    return $res;
}

/* * 转交班主任权限
 * @return mixed
 */

function GiveLeader() {
    $cid = $_POST['cid'];
    $ntid = $_POST['tid'];
    $otid = TEACHER::GetSessionTid();

    $res = myCLASS::GiveLeader($cid, $otid, $ntid);
    return $res;
}

/* * 移除学生
 * @return mixed
 */

function DisStudent() {
    $cid = $_POST['cid'];
    $sids = $_POST['sids'];
    $tid = TEACHER::GetSessionTid();

    $res = myCLASS::DisStudents($cid, $tid, $sids);
    return $res;
}

function AddStudent() {
    $cid = $_POST['cid'];
    $semail = $_POST['semail'];
    $tid = TEACHER::GetSessionTid();

    $res = myCLASS::AddStudent($cid, $tid, $semail);
    return $res;
}

function DisClass() {
    $tid = TEACHER::GetSessionTid();
    $cid = $_POST['cid'];

    $res = myCLASS::DisClass($cid, $tid);
    return $res;
}

function ChangeClassCS() {
    $tid = TEACHER::GetSessionTid();
    $cid = $_POST['cid'];
    $cs = $_POST['cs'];

    $res = myCLASS::ChangeCS($cid, $tid, $cs);
    return $res;
}

/**
 * 修改班级名称
 */
function ChangeClassName() {
    $tid = TEACHER::GetSessionTid();
    $cid = $_POST['cid'];
    $cname = $_POST['cname'];

    $res = myCLASS::ChangeName($cid, $tid, $cname);
    return $res;
}

function GetClassInfo() {
    $res = array();
    $cid = $_POST['cid'];
    $db = getmysql();

    $sql = "select * from class where cid='$cid'";
    $rs = $db->query($sql);
    $rs->setFetchMode(PDO::FETCH_ASSOC);
    $c = $rs->fetch();

    if ($c) {

        $sql = "select * from teacher t, class_teacher ct where t.tid=ct.tid and ct.cid=$cid ORDER by ct.status,ct.jiontime";
        $rs = $db->query($sql);
        $rs->setFetchMode(PDO::FETCH_ASSOC);
        $t = $rs->fetchAll();

        $sql = "select * from student s, class_student cs where s.sid=cs.sid and cs.cid=$cid";
        $rs = $db->query($sql);
        $rs->setFetchMode(PDO::FETCH_ASSOC);
        $s = $rs->fetchAll();

        $res['code'] = 0;
        $res['teacher'] = $t;
        $res['student'] = $s;
        $res['class'] = $c;
    } else {
        global $class_not_exist;
        $res['code'] = 1;
        $res['msg'] = $class_not_exist;
    }
    return $res;
}

?>